﻿using UnityEngine;
using System.Collections.Generic;
using ProtoBuf.Message;

namespace UI
{
    public class NPCUIShopPage : NPCUIPageTemplete
    {
        public UIWidget widget;
        public override void SendCSMsg(object o)
        {
            base.SendCSMsg(o);

            UIUnionFightRunMap.GetNetData().SendCSUFShopList(dictData.UFNId+"");
            GetComponent<PageTweener>().Play();
        }
        public void UpdateItemList(List<InstUFShopItem> items)
        {
            if (items.Count <= 0)
                return;
            page.SetActive(true);

            items.Sort(delegate (InstUFShopItem a, InstUFShopItem b)
            {
                if (a.priority > b.priority)
                    return 1;
                else if (a.priority < b.priority)
                    return -1;
                return 0;
            });
            foreach (var item in simpleGrid.childs)
            {
                Destroy(item);
            }
            simpleGrid.childs.Clear();

            pref.SetActive(false);
            for (int i = 0; i < items.Count; i++)
            {
                GameObject obj = Instantiate(pref);
                obj.SetActive(true);
                var script = obj.GetComponent<NPCUIShopItem>();
                script.SetItem(items[i],this);
                simpleGrid.AddChild(obj);
            }
            widget.height = Mathf.CeilToInt(items.Count / 2f) * (int)simpleGrid.cellHeight;
        }
    }

}

